﻿using System;
using UnityEngine;

// Token: 0x020003F4 RID: 1012
public class MeshBlend : MonoBehaviour
{
	// Token: 0x0600194E RID: 6478 RVA: 0x000A3B00 File Offset: 0x000A1D00
	private void OnEnable()
	{
		this.Setup();
	}

	// Token: 0x0600194F RID: 6479 RVA: 0x000A3B08 File Offset: 0x000A1D08
	private void Update()
	{
	}

	// Token: 0x06001950 RID: 6480 RVA: 0x000A3B0C File Offset: 0x000A1D0C
	private void OnValidate()
	{
		if (!this.isBlendEnable)
		{
			this.Setup();
		}
		this.Calc();
	}

	// Token: 0x06001951 RID: 6481 RVA: 0x000A3B28 File Offset: 0x000A1D28
	private void Setup()
	{
		if (this.baseRenderer && this.blendRenderer)
		{
			this.baseMesh = MeshBlend.CloneMesh(this.baseRenderer.gameObject);
			Mesh mesh = MeshBlend.GetMesh(this.blendRenderer.gameObject);
			if (this.baseMesh == null)
			{
				GlobalData.Debug_LogWarning("baseMesh null");
			}
			if (mesh == null)
			{
				GlobalData.Debug_LogWarning("blendMesh null");
			}
			this.baseNormals = this.baseMesh.normals;
			this.baseUVs = this.baseMesh.uv;
			this.blendNormals = mesh.normals;
			this.blendUVs = mesh.uv;
			if (this.baseNormals.Length == this.blendNormals.Length && this.baseUVs.Length == this.blendUVs.Length)
			{
				this.isBlendEnable = true;
			}
			else
			{
				this.isBlendEnable = false;
				GlobalData.Debug_LogWarning("网格混合的数据数量不一致");
			}
			this.blendRenderer.gameObject.SetActive(false);
			this.Calc();
		}
	}

	// Token: 0x06001952 RID: 6482 RVA: 0x000A3C48 File Offset: 0x000A1E48
	private void Calc()
	{
		if (this.isBlendEnable)
		{
			if (this.changeNormal)
			{
				Vector3[] array = new Vector3[this.baseNormals.Length];
				for (int i = 0; i < this.baseNormals.Length; i++)
				{
					array[i] = Vector3.Lerp(this.baseNormals[i], this.blendNormals[i], this.rate);
				}
				this.baseMesh.normals = array;
			}
			if (this.changeUV)
			{
				Vector2[] array2 = new Vector2[this.baseUVs.Length];
				for (int j = 0; j < this.baseUVs.Length; j++)
				{
					array2[j] = Vector3.Lerp(this.baseUVs[j], this.blendUVs[j], this.rate);
				}
				this.baseMesh.uv = array2;
			}
		}
	}

	// Token: 0x06001953 RID: 6483 RVA: 0x000A3D60 File Offset: 0x000A1F60
	public void Change(float rate)
	{
		this.rate = rate;
		this.Calc();
	}

	// Token: 0x06001954 RID: 6484 RVA: 0x000A3D70 File Offset: 0x000A1F70
	public static Mesh CloneMesh(GameObject OriginalData)
	{
		MeshFilter meshFilter = OriginalData.GetComponent(typeof(MeshFilter)) as MeshFilter;
		if (null != meshFilter)
		{
			Mesh mesh = UnityEngine.Object.Instantiate<Mesh>(meshFilter.sharedMesh);
			mesh.name = meshFilter.sharedMesh.name;
			meshFilter.sharedMesh = mesh;
			return meshFilter.sharedMesh;
		}
		SkinnedMeshRenderer component = OriginalData.GetComponent<SkinnedMeshRenderer>();
		if (component && component.sharedMesh)
		{
			Mesh mesh2 = UnityEngine.Object.Instantiate<Mesh>(component.sharedMesh);
			mesh2.name = component.sharedMesh.name;
			component.sharedMesh = mesh2;
			return component.sharedMesh;
		}
		return null;
	}

	// Token: 0x06001955 RID: 6485 RVA: 0x000A3E18 File Offset: 0x000A2018
	public static Mesh GetMesh(GameObject OriginalData)
	{
		MeshFilter component = OriginalData.GetComponent<MeshFilter>();
		if (null != component)
		{
			return component.mesh;
		}
		SkinnedMeshRenderer component2 = OriginalData.GetComponent<SkinnedMeshRenderer>();
		if (component2 && component2.sharedMesh)
		{
			return component2.sharedMesh;
		}
		return null;
	}

	// Token: 0x04001C92 RID: 7314
	[SerializeField]
	private Renderer baseRenderer;

	// Token: 0x04001C93 RID: 7315
	[SerializeField]
	private Renderer blendRenderer;

	// Token: 0x04001C94 RID: 7316
	[Range(0f, 1f)]
	[SerializeField]
	private float rate;

	// Token: 0x04001C95 RID: 7317
	[SerializeField]
	private bool changeUV = true;

	// Token: 0x04001C96 RID: 7318
	[SerializeField]
	private bool changeNormal = true;

	// Token: 0x04001C97 RID: 7319
	private Vector3[] baseNormals;

	// Token: 0x04001C98 RID: 7320
	private Vector3[] blendNormals;

	// Token: 0x04001C99 RID: 7321
	private Vector2[] baseUVs;

	// Token: 0x04001C9A RID: 7322
	private Vector2[] blendUVs;

	// Token: 0x04001C9B RID: 7323
	private bool isBlendEnable;

	// Token: 0x04001C9C RID: 7324
	private Mesh baseMesh;
}
